import Vuex from 'vuex';
import user from './user.js';
import cart from './cart.js';
import address from './address.js';
import order from './order.js';
import images from './images.js';
import history from './history.js';



const myPlugin=(store)=>{
  store.subscribe((mutations,state)=>{
      if (mutations.type.startsWith('cart/')) {
          sessionStorage.setItem('cartList',JSON.stringify(state.cart.cartList))
      }
      if (mutations.type.startsWith('address/')) {
          sessionStorage.setItem('addressList',JSON.stringify(state.address.addressList))
      }
  })
}
export default new Vuex.Store({
  state:{
    routerType:'push'
  },
  mutations:{
    changeRouterType(state,newRouterType){
      state.routerType=newRouterType
    }
  },
  getters:{
    routerType:(state)=>state.routerType
  },
  modules: {
    user,
    cart,
    address,
    order,
    images,
    history
  },
  plugins:[myPlugin]
})
